package com.wandian.view.ui
{
	import com.greensock.TweenLite;
	import com.wandian.util.GDI;
	import com.wandian.util.Res;

	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;


	/**
	 * 动画按钮，实现点击位移1
	 * @author long
	 */
	public class AnimationButton extends Sprite
	{
		private var key:String;
		private var clickHandler:Function
		private var enable:Boolean=true;
		private var varsdown:Object;
		private var varsup:Object;
		private var bitmapdata:BitmapData;
		/**
		 *
		 * @default
		 */
		public var booladdEventListener:Boolean=false;

		/**
		 *
		 * @param key
		 * @param _varsdown
		 * @param _varsup
		 */
		public function AnimationButton(key:String, _varsdown:Object, _varsup:Object)
		{
			super();
			this.varsdown=_varsdown;
			this.varsup=_varsup;
			this.x=_varsdown.x;
			this.y=_varsdown.y;
			this.useHandCursor=true;
			setKey(key);
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			this.addEventListener(MouseEvent.MOUSE_OUT, onUp);
			this.addEventListener(MouseEvent.MOUSE_UP, onUp);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.addEventListener(MouseEvent.CLICK, onClick);

		}

		/**
		 *
		 * @param key
		 */
		public function setKey(key:String):void
		{
			this.key=key
			onUp();
		}

		/**
		 *
		 */
		public function addBtnEventListener():void
		{
			this.addEventListener(MouseEvent.CLICK, onClick);
		}

		/**
		 *
		 */
		public function removeBtnEventListener():void
		{
			this.removeEventListener(MouseEvent.CLICK, onClick);
		}


		/**
		 *
		 * @param handler
		 */
		public function setClickHandler(handler:Function):void
		{
			this.clickHandler=handler
		}

		private function onMove(e:MouseEvent):void
		{
			if (!enable || !bitmapdata.getPixel32(e.localX, +e.localY))
			{
				this.buttonMode=false;
				return
			}
			this.buttonMode=true;
		}

		private function onOver(e:MouseEvent):void
		{
			if (!enable)
			{
				return
			}
			var bmd:BitmapData=Res.get(key + "_btn_over")
			if (bmd == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();

			GDI.drawBitmapData(g, 0, 0, bmd);
		}

		private function onUp(e:MouseEvent=null):void
		{
			if (!enable)
			{
				return
			}
			bitmapdata=Res.get(key + "_btn");
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, bitmapdata);
			if (this.varsdown.x != this.x || this.varsdown.y != this.y)
				TweenLite.to(this, 0.1, this.varsdown);

		}

		private function onDown(e:MouseEvent=null):void
		{
			if (!enable || !bitmapdata.getPixel32(e.localX, +e.localY))
			{
				return
			}
			bitmapdata=Res.get(key + "_btn");
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, bitmapdata);
			if (this.varsup.x != this.x || this.varsup.y != this.y)
				TweenLite.to(this, 0.1, this.varsup);

		}

		private function onDisable():void
		{

			bitmapdata=Res.get(key + "_btn_disable")
			if (bitmapdata == null)
			{
				return
			}
			var g:Graphics=this.graphics
			g.clear();
			GDI.drawBitmapData(g, 0, 0, bitmapdata);

		}

		private function onClick(e:MouseEvent):void
		{
			if (!enable || !bitmapdata.getPixel32(e.localX, +e.localY))
			{
				//trace(e.localX + ":" + e.localY + " pixel:" + bitmapdata.getPixel32(e.localX, +e.localY));
				this.booladdEventListener=false;
				return
			}
			this.booladdEventListener=true;
			//var bm:BlitMask = new BlitMask(this, this.x, this.y, 514, 350, false);	
			if (clickHandler != null)
			{
				this.clickHandler(e);
			}
		}

		/**
		 *
		 * @param f
		 */
		public function setEnable(f:Boolean):void
		{
			enable=f
			if (enable)
			{
				onUp();
			}
			else
			{
				onDisable();
			}
		}

		/**
		 *
		 * @return
		 */
		public function getEnable():Boolean
		{
			return enable;

		}
	}
}
